package com.wb3.organizer.converter;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Map;

import org.apache.struts2.util.StrutsTypeConverter;

/**
 * Struts2 Date类型转换,提供字符串与Date类型互相转换
 * @author pharen
 */
public class DateTypeConverter extends StrutsTypeConverter {
	
	//字符串格式为 "年-月-日"
	private static SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");

	//从字符串转换成Date类型时调用
	@Override
	public Object convertFromString(Map context, String[] value, Class toType) {
		try {
			return sf.parse(value[0]);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}

	//从Date转换成字符串时调用
	@Override
	public String convertToString(Map context, Object value) {
		return sf.format(value);
	}

}
